<template>
  <div class="login">
    <div class="login-container">
      <div class="login-header">
        <div class="system-logo">
          <img src="@/assets/images/credit-card-icon.svg" alt="信用卡风控系统" class="system-logo-img">
        </div>
        <h1 class="system-title">{{title}}</h1>
        <p class="system-subtitle">智能风控 · 安全保障</p>
      </div>
      
      <div class="login-form-container">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
          <h3 class="title">系统登录</h3>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              placeholder="请输入用户名"
            >
              <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="请输入密码"
              @keyup.enter.native="handleLogin"
              show-password
            >
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="code" v-if="captchaEnabled">
            <el-input
              v-model="loginForm.code"
              auto-complete="off"
              placeholder="验证码"
              style="width: 63%"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img"/>
            </div>
          </el-form-item>
          <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
          <el-form-item style="width:100%;">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              style="width:100%;"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
            <div style="float: right;" v-if="register">
              <router-link class="link-type" :to="'/register'">立即注册</router-link>
            </div>
          </el-form-item>
          
          <!-- 安全提示 -->
          <div class="security-tips">
            <i class="el-icon-lock"></i>
            <span>您的信息将被加密传输，请放心使用</span>
          </div>
        </el-form>
      </div>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2025 基于机器学习的信用卡风控系统 All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      title: process.env.VUE_APP_TITLE,
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    getCookie() {
      const username = Cookies.get("username")
      const password = Cookies.get("password")
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 })
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove("username")
            Cookies.remove("password")
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background: linear-gradient(135deg, #1a365d 0%, #2c5282 50%, #2b6cb0 100%);
  overflow: hidden;
  position: relative;
  
  &::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image: url("../assets/images/credit-risk-background.svg");
    background-size: cover;
    background-position: center;
    opacity: 0.2;
    z-index: 0;
  }
}

.login-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 1000px;
  z-index: 1;
  position: relative;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
  color: #fff;
  
  .system-logo {
    margin-bottom: 20px;
    
    .system-logo-img {
      width: 64px;
      height: 64px;
      animation: pulse 2s infinite ease-in-out;
      filter: drop-shadow(0 0 10px rgba(99, 179, 237, 0.5));
    }
  }
  
  .system-title {
    font-size: 32px;
    font-weight: 600;
    margin: 0 0 10px;
    letter-spacing: 1px;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  }
  
  .system-subtitle {
    font-size: 16px;
    font-weight: 300;
    margin: 0;
    opacity: 0.9;
    letter-spacing: 2px;
  }
}

.login-form-container {
  width: 100%;
  max-width: 400px;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #2c5282;
  font-weight: 600;
  font-size: 24px;
}

.login-form {
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  width: 100%;
  padding: 40px 30px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1), 0 5px 15px rgba(0, 0, 0, 0.07);
  border: 1px solid rgba(255, 255, 255, 0.2);
  
  .el-input {
    height: 44px;
    
    input {
      height: 44px;
      border-radius: 6px;
      border: 1px solid #e2e8f0;
      padding-left: 40px;
      
      &:focus {
        border-color: #4299e1;
        box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.1);
      }
    }
  }
  
  .input-icon {
    height: 44px;
    width: 16px;
    margin-left: 12px;
    color: #718096;
  }
  
  .el-form-item {
    margin-bottom: 20px;
    
    &.is-error .el-input__inner {
      border-color: #f56565;
    }
  }
  
  .el-checkbox {
    color: #4a5568;
    
    .el-checkbox__input.is-checked .el-checkbox__inner {
      background-color: #4299e1;
      border-color: #4299e1;
    }
  }
  
  .el-button--primary {
    background: linear-gradient(135deg, #3182ce 0%, #2c5282 100%);
    border: none;
    height: 44px;
    font-size: 16px;
    font-weight: 500;
    border-radius: 6px;
    transition: all 0.3s ease;
    
    &:hover, &:focus {
      background: linear-gradient(135deg, #2c5282 0%, #2a4e7c 100%);
      transform: translateY(-2px);
      box-shadow: 0 7px 14px rgba(50, 50, 93, 0.1), 0 3px 6px rgba(0, 0, 0, 0.08);
    }
    
    &:active {
      transform: translateY(1px);
    }
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 44px;
  float: right;
  
  img {
    cursor: pointer;
    vertical-align: middle;
    height: 44px;
    border-radius: 4px;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: rgba(255, 255, 255, 0.7);
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
  z-index: 1;
}

.login-code-img {
  height: 44px;
}

.link-type {
    color: #4299e1;
    
    &:hover {
      color: #2b6cb0;
    }
  }
  
  .security-tips {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 20px;
    color: #718096;
    font-size: 12px;
    
    i {
      margin-right: 5px;
      color: #48bb78;
    }
  }

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
  }
}
</style>
